package montecarlothreads.singlethread;
import java.util.Random;
import javax.swing.JOptionPane;
import montecarlothreads.MonteCarloThreads;
//Single thread algorithm for monte carlo
public class single extends Thread {

        public static int i = 0;
	public single(  String CallPutFlag,
                        double s,
                        double x,
                        double r, 
                        double t,
                        double b,
                        double v,
			int nbSteps,
                        int nbSimulation
                        ){
                //Start timing
                long startTimeMs = System.currentTimeMillis();
                //Declarations, initializations
                double st;
                Random random = new Random();

                double sum = 0;
                double dt = t/nbSteps;
		double drift = dt*(b-java.lang.Math.pow(v,2)/2);
		double vSqrdt = v*java.lang.Math.sqrt(dt);
                
                int z = 0;
		if(CallPutFlag.equals("c")){
		z = 1;
		}
                else if (CallPutFlag.equals("p")){
		z = -1;
		}
                for (i=0; i < nbSimulation ; i ++){
                    st = s;
                    for(int j = 0 ; j < nbSteps ; j++){
                        st = st * Math.exp(drift + vSqrdt * random.nextGaussian());
			}
                    sum = sum + Math.max(z*(st-x),0);
                    //Update progress bar
                    MonteCarloThreads.mainFrame.jProgressBar1.setValue(i/((nbSimulation-1)/100));
                }
                double monteCarlo = Math.exp((-r)*t)*(sum/nbSimulation);
                //Stop timing
                long endTimeMs = System.currentTimeMillis();
                //Message
                JOptionPane.showMessageDialog(MonteCarloThreads.mainFrame, "Result : " + monteCarlo 
                                                                        + "\n   Time : " + (endTimeMs - startTimeMs)+" ms");
                //Progress bar back to 0
                MonteCarloThreads.mainFrame.jProgressBar1.setValue(0);
        }
}